package com.whq.knowverywell.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.whq.knowverywell.R;
import com.whq.knowverywell.business.model.ProductSalesRecord;
import com.whq.knowverywell.util.LogUtil;
import com.whq.knowverywell.util.StringUtil;
import com.whq.knowverywell.util.constant.ModelConstant;

/**
 * detail list adapter
 * 
 * @author daiwei
 * 
 */
@SuppressWarnings(value = "all")
public class DetailListAdapter extends BaseAdapter {
    private static final String TAG = DetailListAdapter.class.getSimpleName();

    private LayoutInflater inflater;
    private Activity ctx;
    private List resultList;
    private int type;

    public DetailListAdapter(Activity ctx, List resultList, int type) {
        this.type = type;
        this.resultList = resultList;
        this.ctx = ctx;

        try {
            this.inflater = LayoutInflater.from(ctx);
        } catch (Exception e) {
            LogUtil.e(TAG, "exception:", e);
        }
        if (this.resultList == null) {
            this.resultList = new ArrayList();
        }
    }

    @Override
    public int getCount() {
        return resultList.size();
    }

    @Override
    public Object getItem(int position) {
        return resultList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DetailViewHolder detailViewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.detail_item, null);
            detailViewHolder = new DetailViewHolder();
            detailViewHolder.productNoTextView = (TextView) convertView.findViewById(R.id.product_no_textview);
            detailViewHolder.discountTextView = (TextView) convertView.findViewById(R.id.disaccount_textview);
            detailViewHolder.sellAmountTextView = (TextView) convertView.findViewById(R.id.sell_amount_textview);
            detailViewHolder.selledReservedCountTextView = (TextView) convertView
                    .findViewById(R.id.selled_reserve_textview);

            convertView.setTag(detailViewHolder);
        } else {
            detailViewHolder = (DetailViewHolder) convertView.getTag();
        }

        AQuery aq = new AQuery(convertView);
        switch (type) {
        case ModelConstant.BUSINESS_TYPE_HOTSELL:
            ProductSalesRecord record = (ProductSalesRecord) getItem(position);
            if (record == null) {
                LogUtil.e(TAG, "the position[" + position + "] item is null .");
                return convertView;
            }

            detailViewHolder.productNoTextView.setText(record.getCode());
            detailViewHolder.discountTextView.setText(record.getAverageDiscount() + "");
            detailViewHolder.sellAmountTextView.setText("￥ " + StringUtil.formatAmount(record.getSalesAmount()));
            detailViewHolder.selledReservedCountTextView.setText("" + record.getSalesVolume() + "/" + record.getStockQty() + "");

            String imageUrl = record.getImageUrl();
            // imageUrl =
            // "http://farm3.static.flickr.com/2199/2218403922_062bc3bcf2.jpg";
            LogUtil.d(TAG, "imageUrl[" + imageUrl + "]");
            aq.id(R.id.product_imageview).image(imageUrl, true, true, 90, 0, null, AQuery.FADE_IN, 1.0f);

            break;

        case ModelConstant.BUSINESS_TYPE_SUMMARY:
        case ModelConstant.BUSINESS_TYPE_DOMAIN:
        case ModelConstant.BUSINESS_TYPE_SORT:
            ProductSalesRecord summaryDetail = (ProductSalesRecord) getItem(position);
            if (summaryDetail == null) {
                LogUtil.e(TAG, "the position[" + position + "] item is null .");
                return convertView;
            }

            detailViewHolder.productNoTextView.setText(summaryDetail.getCode());
            detailViewHolder.discountTextView.setText(summaryDetail.getAverageDiscount() + "");
            detailViewHolder.sellAmountTextView.setText("￥ " + StringUtil.formatAmount(summaryDetail.getSalesAmount()));
            detailViewHolder.selledReservedCountTextView.setText("" + summaryDetail.getSalesVolume() + "/" + summaryDetail.getStockQty() + "");

            imageUrl = summaryDetail.getImageUrl();
            // imageUrl =
            // "http://farm3.static.flickr.com/2199/2218403922_062bc3bcf2.jpg";
            LogUtil.d(TAG, "imageUrl[" + imageUrl + "]");
            aq.id(R.id.product_imageview).image(imageUrl, true, true, 90, 0, null, AQuery.FADE_IN, 1.0f);

            break;

        default:
            break;
        }

        return convertView;
    }

    class DetailViewHolder {
        TextView productNoTextView, discountTextView, sellAmountTextView, selledReservedCountTextView;
        ImageView productImageView;
    }
}
